package merchant.space;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

public class Item {

	private int collection_id;
	private int id;
	private int photo_id;
	private String title;
	private String description;
	private Bitmap photo = null;
	private Bitmap thumb = null;
	private String url_thumb = null;
	private String url_photo = null;
	
	
	public Item(int c,int i,String t_url,String t,boolean tr,boolean sel,String desc,String url_ph)
	{
		this.collection_id=c;
		this.id=i;
		this.url_thumb=t_url;
		this.title=t;
		this.tradeable=tr;
		seleable=sel;
		description=desc;
		url_photo=url_ph;
		getThumb();
		
	}
	
	
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	private boolean tradeable,seleable;
	
	public static Bitmap minimize(String path)
	{
		Bitmap aux = BitmapFactory.decodeFile(path);
		int width=64,height=64;
		
		int imgWidth = aux.getWidth();
		int imgHeight = aux.getHeight();

		// Constrain to given size but keep aspect ratio
		float scaleFactor = Math.min(((float) width) / imgWidth, ((float) height) / imgHeight);

		Matrix scale = new Matrix();
		scale.postScale(scaleFactor, scaleFactor);
		return Bitmap.createBitmap(aux, 0, 0, imgWidth, imgHeight, scale, false);

		
	}
	
	public static Bitmap preview(String path)
	{
		Bitmap aux = BitmapFactory.decodeFile(path);
		int width=128,height=128;
		
		int imgWidth = aux.getWidth();
		int imgHeight = aux.getHeight();

		// Constrain to given size but keep aspect ratio
		float scaleFactor = Math.min(((float) width) / imgWidth, ((float) height) / imgHeight);

		Matrix scale = new Matrix();
		scale.postScale(scaleFactor, scaleFactor);
		return Bitmap.createBitmap(aux, 0, 0, imgWidth, imgHeight, scale, false);

		
	}
	
	public Bitmap getThumb() {
        
		if (thumb!=null) return thumb;
		else
		{
			if(id==50)
			{
				Bitmap aux = BitmapFactory.decodeFile(url_thumb);
				int width=64,height=64;
				
				int imgWidth = aux.getWidth();
				int imgHeight = aux.getHeight();

				// Constrain to given size but keep aspect ratio
				float scaleFactor = Math.min(((float) width) / imgWidth, ((float) height) / imgHeight);

				Matrix scale = new Matrix();
				scale.postScale(scaleFactor, scaleFactor);
				thumb = Bitmap.createBitmap(aux, 0, 0, imgWidth, imgHeight, scale, false);

				//thumb.recycle();

				return thumb;
				
			}
			
			else
			{
			try {
				String url;
				if (url_thumb.equals("/missing_thumb.png"))
					url = "http://merchant-ldso.heroku.com/missing_thumb.png";
				else 
					url=url_thumb;
	        	
				Log.d("HUMB",url);
				HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
		        conn.setDoInput(true);	
				conn.connect();
				thumb = BitmapFactory.decodeStream(conn.getInputStream());
				return thumb;
			} catch (IOException e) {}
		return null;
		} }
	}
	
public Bitmap getPhoto() {
        
		if (photo!=null) return photo;
		else
		{
			
			if(id==50)
			{
				
				photo = BitmapFactory.decodeFile(url_thumb);
			
				return photo;
				
			}
			
			
			try {
				String url;
				if (url_photo.equals("/missing_original.png"))
					url = "http://merchant-ldso.heroku.com/missing_original.png";
				else 
					url=url_photo;
				HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
		        conn.setDoInput(true);	
				conn.connect();
				photo = BitmapFactory.decodeStream(conn.getInputStream());
				return photo;
			} catch (IOException e) {}
		return null;
		}
	}

	public File getThumbFile()
	{
		return new File(url_thumb);
	}
	
	public File getPhotoFile()
	{
		return new File(url_thumb);
	}
	
	

	public int getCollection_id() {
		return collection_id;
	}

	public void setCollection_id(int collection_id) {
		this.collection_id = collection_id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getPhoto_id() {
		return photo_id;
	}

	public void setPhoto_id(int photo_id) {
		this.photo_id = photo_id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public boolean isTradeable() {
		return tradeable;
	}

	public void setTradeable(boolean tradeable) {
		this.tradeable = tradeable;
	}
	
	public boolean isSeleable() {
		return seleable;
	}

	public void setSeleable(boolean seleable) {
		this.seleable = seleable;
	}
	
	
	public String getThumbString() { return url_thumb; }
	
	
	
}
